package com.co;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张老师
 * @date 2021/4/17
 */
public class SearchExpressNo {
    
    @Test
    public void expressTest() throws IOException {
        
        StringBuilder url= new StringBuilder("https://jisuexpress.api.bdymkt.com/express/query");
        url.append("?").append("type=").append("auto")
                .append("&").append("number=").append("75454479418202")
                .append("&").append("mobile=").append("15129210947");
        OkHttpClient okhttpclient = new OkHttpClient.Builder().build();
        RequestBody requestbody = RequestBody.create("",MediaType.parse("text/html"));
        Request request = new Request.Builder()
                .url(url.toString())
                .method("POST",requestbody)
                .addHeader("X-Bce-Signature","AppCode/053e1d7b056545bca03483b7e0b21ed0")
                .build();
        Response response = okhttpclient.newCall(request).execute();
        Gson gson= new Gson();
        JsonObject jsonObject =gson.fromJson(response.body().string(),JsonObject.class);
        
        //断言
        int status = jsonObject.get("status").getAsInt();
        String msg = jsonObject.get("msg").getAsString();
        JsonObject obj= jsonObject.get("result").getAsJsonObject();
        List<JsonObject>  list = new ArrayList<JsonObject>();
        for (int i=0;i<obj.get("list").getAsJsonArray().size();i++) {
            //System.out.println(obj.get("list").getAsJsonArray().get(i));
            list.add(obj.get("list").getAsJsonArray().get(i).getAsJsonObject());
        }
        
        Assert.assertEquals("2021-04-12 19:31:26",list.get(0).get("time").getAsString());
//        System.out.println(obj);
        Assert.assertTrue(response.code() == 200);
        Assert.assertTrue(status==0);
        Assert.assertEquals("ok",msg);
        Assert.assertNotNull(obj);
//        Assert.fail("shi bai le");
    }
}
